package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.bean.Admin;
import com.hairline.bean.AdminExample;
import com.hairline.bean.Role;
import com.hairline.bean.RoleExample;
import com.hairline.bean.bo.RolePermissionsBO;
import com.hairline.bean.vo.AdminListVO;
import com.hairline.bean.vo.OptionsVO;
import com.hairline.bean.vo.permission.PermissionVO;
import com.hairline.bean.vo.permission.PermissionsId;
import com.hairline.bean.vo.permission.SystemPermission;
import com.hairline.mapper.AdminMapper;
import com.hairline.mapper.PermissionAllMapper;
import com.hairline.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AdminRoleServiceImpl implements AdminRoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    PermissionAllMapper permissionAllMapper;

    @Override
    public Map<String, Object> roleList(int page, int limit, String sort, String order, String name) {
        PageHelper.startPage(page, limit);
        Map<String, Object> map = new HashMap<>();
        RoleExample roleExample = new RoleExample();
        //添加排序方式
        roleExample.setOrderByClause(sort+ " " + order);
        RoleExample.Criteria criteria = roleExample.createCriteria();
        //只显示未被删除的角色
        criteria.andDeletedNotEqualTo(true);
        //名字不为空
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        List<Role> roles = roleMapper.selectByExample(roleExample);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        long total = pageInfo.getTotal();
        map.put("total", total);
        map.put("items", roles);
        return map;
    }

    @Override
    public Role roleCreate(Role role) {
        String name = role.getName();
        List<Role> roles = roleMapper.selectByExample(null);
        for (Role role1 : roles) {
            if (role1.getName().equals(name)) {
                return null;
            }
        }
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        //添加
        roleMapper.insertSelective(role);
        int lastInsertKey = roleMapper.selectLastInsertId();
        Role role1 = roleMapper.selectByPrimaryKey(lastInsertKey);
        //在关系表中添加
        permissionAllMapper.insertRole(role1.getId());
        return role1;
    }

    @Override
    public int roleUpdate(Role role) {
        //检验用户名是否重复
        RoleExample roleExample1 = new RoleExample();
        //排除自身
        roleExample1.createCriteria().andIdNotEqualTo(role.getId());
        List<Role> roles = roleMapper.selectByExample(roleExample1);
        for (Role role1 : roles) {
            if (role1.getName().equals(role.getName())) {
                return 404;
            }
        }
        Role role1 = new Role();
        role1.setId(role.getId());
        role1.setName(role.getName());
        role1.setDesc(role.getDesc());
        role1.setUpdateTime(new Date());
        roleMapper.updateByPrimaryKeySelective(role1);
        return 0;
    }

    @Override
    public void roleDelete(Role role) {
        Role role1 = new Role();
        role1.setId(role.getId());
        role1.setDeleted(true);
        roleMapper.updateByPrimaryKeySelective(role1);
    }

    @Override
    public Map<String, Object> adminList(int page, int limit, String sort, String order, String username) {
        PageHelper.startPage(page, limit);
        AdminExample adminExample = new AdminExample();
        //添加排序
        adminExample.setOrderByClause(sort + " " + order);
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        List<AdminListVO> list = new ArrayList<>();
        for (Admin admin : admins) {
            AdminListVO adminListVO = new AdminListVO(admin.getId(), admin.getUsername(), admin.getAvatar(), admin.getRoleIds());
            list.add(adminListVO);
        }
        PageInfo<AdminListVO> pageInfo = new PageInfo<>();
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("items", list);
        return map;
    }

    @Override
    public List<OptionsVO> roleOptions() {
        List<Role> roles = roleMapper.selectByExample(null);
        List<OptionsVO> list = new ArrayList<>();
        for (Role role : roles) {
            OptionsVO optionsVO = new OptionsVO(role.getId(), role.getName());
            list.add(optionsVO);
        }
        return list;

    }

    @Override
    public PermissionVO rolePermissions(int roleId) {
        PermissionVO permissionVO = new PermissionVO();
        //从cskaoyanmall_permission_all所有权限表中查询所有权限,需要和p1、p2连表
        //这里写了好几个vo，没有逆向我不会弄在一个类里，嘿嘿
        List<SystemPermission> s = permissionAllMapper.selectAllPermission();
        permissionVO.setSystemPermissions(s);
        //查询当前权限
        //首先查询role_permission关系表中对应roleId拥有的权限id
        //type_handler转换使用对象包裹Integer[]
        PermissionsId permissionsId = permissionAllMapper.selectPermissionIds(roleId);
        String[] a = null;
        //根据关系表中的id去cskaoyanmall_permission_all查询对应权限的名字，比如 1 对应 admin:admin:update
        if (permissionsId != null) {
            a = permissionAllMapper.selectAssignedPermissions(permissionsId.getIds());
        }
        permissionVO.setAssignedPermissions(a);
        return permissionVO;
    }

    @Override
    public void updatePermissions(RolePermissionsBO rolePermissionsBO) {
        System.out.println(Arrays.toString(rolePermissionsBO.getPermissions()));
//        Integer[] ids = null;
//        if (rolePermissionsBO.getPermissions().length != 0) {
        Integer[] ids = permissionAllMapper.selectNewIds(rolePermissionsBO.getPermissions());
//        }
        System.out.println(Arrays.toString(ids));
        permissionAllMapper.updatePermission(ids, rolePermissionsBO.getRoleId());
    }
}
